package com.person.leetcode.editor.cn;

/**
 * 2.两数相加
 * 2022-03-30 17:52:09
 **/
public class Sequence_0002_AddTwoNumbers {
    public static void main(String[] args) {
        Solution solution = new Sequence_0002_AddTwoNumbers().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     */

    class Solution {
        public ListNode next(ListNode l1, ListNode l2, int nextVal) {
            int val1 = l1 == null ? 0 : l1.val;
            int val2 = l2 == null ? 0 : l2.val;
            ListNode next1 = l1 == null ? null : l1.next;
            ListNode next2 = l2 == null ? null : l2.next;
            int val = val1 + val2 + nextVal;
            if (null == next1 && null == next2 && 0 == val / 10) {
                return new ListNode(val % 10);
            } else {
                return new ListNode(val % 10, next(next1, next2, val / 10));
            }
        }

        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode listNode = new ListNode();
            int val1 = l1 == null ? 0 : l1.val;
            int val2 = l2 == null ? 0 : l2.val;
            ListNode next1 = l1 == null ? null : l1.next;
            ListNode next2 = l2 == null ? null : l2.next;
            int t = val1 + val2;
            listNode.val = t % 10;
            if (null == next1 && null == next2 && 0 == t / 10) {
                return listNode;
            } else {
                listNode.next = next(next1, next2, t / 10);
                return listNode;
            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
